package com.lj.controller;

import com.github.pagehelper.PageInfo;
import com.lj.pojo.Stafflj;
import com.lj.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@Controller
@RequestMapping("/staff")
public class StaffAction {
    public static final int PAGE_SIZE=5;
    @Autowired
    StaffService staffService;
    @RequestMapping("/selectStaff")
    public String selectStaff(HttpServletRequest request){
        //得到第一页数据
        PageInfo info=staffService.selectStaff(1,PAGE_SIZE);
        request.setAttribute("info",info);
        return "admin/staffView";
    }
    //ajax分页翻页处理
    @ResponseBody
    @RequestMapping("/ajaxSplit")
    public void ajaxSplit(int page, HttpSession session){
        //取得当前page参数页面的数据
        PageInfo info=staffService.selectStaff(page,PAGE_SIZE);
        session.setAttribute("info",info);
    }
    @RequestMapping(value = "/insert")
    public String insert(Stafflj stafflj){
        if(stafflj.getClientId().equals("null")){
            stafflj.setClientId(null);
        }
        if(stafflj.getDepartmentName().equals("null")){
            stafflj.setDepartmentName(null);
        }
        if(stafflj.getProjectName().equals("null")){
            stafflj.setProjectName(null);
        }
        int num=-1;
        num=staffService.insertStaff(stafflj);
        return "redirect:/staff/selectStaff.action";
    }
    @ResponseBody
    @RequestMapping("/getStaffById")
    public Stafflj getStaffById(String staffId, HttpServletRequest request ){
        Stafflj stafflj=staffService.getById(staffId);
        return stafflj;
    }
    @RequestMapping("/delete")
    public String delete(String staffId){
        int nums=-1;
        nums=staffService.delete(staffId);
        return"forward:/staff/selectStaff.action";
    }
    @RequestMapping("/update")
    public String update(Stafflj stafflj){
        if(stafflj.getProjectName().equals("null")){
            stafflj.setProjectName(null);
        }
        if(stafflj.getClientId().equals("null")){
            stafflj.setClientId(null);
        }
        if(stafflj.getDepartmentName().equals("null")){
            stafflj.setDepartmentName(null);
        }
        int nums= staffService.update(stafflj);
        return "redirect:/staff/selectStaff.action";
    }


}
